package com.lin.controller.controller;

import com.lin.controller.commons.AppCode;
import com.lin.controller.commons.exception.APIException;
import com.lin.controller.commons.ResultVo;
import com.lin.controller.service.UserService;
import com.lin.controller.vo.UserParams;
import com.lin.controller.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * @ClassName ApiController
 * @Description
 * @Author lin
 * @Date 2022/7/26 下午11:22
 * @Version V1.0
 */
@RestController
@RequestMapping("/api")
public class ApiController {

    @Autowired
    private UserService userService;

    @GetMapping("/users/{name}")
    public ResultVo findByName(@NotBlank @PathVariable("name") String name){
        return new ResultVo(userService.findByName(name));
    };

    @PostMapping("/users")
    public List<UserVO> findByVo(@Validated @RequestBody UserParams params){
        return userService.findByName(params.getName());
    }

    @GetMapping("/user/exception")
    public List<UserVO> throwException(){
        throw new APIException(AppCode.APP_ERROR,"自定义业务异常");
    }
}
